﻿using System;
using System.Windows;
using System.Windows.Controls;
using Gao.Poker.App.Print;
using Gao.Poker.App.ViewModel;
using Gao.Poker.Domain;
using Gao.Poker.Services;

namespace Gao.Poker.App.Game
{
    /// <summary>
    /// Interaction logic for ApplyView.xaml
    /// </summary>
    public partial class ApplyView : UserControl
    {
        public GameViewModel ViewModel { get; set; }

        public Action<Boolean> CallBack;

        public ApplyView()
        {
            InitializeComponent();

            btnSearch.Click += Search_Click;
            btnSubmit.Click += Submit_Click;
        }

        void Submit_Click(object sender, RoutedEventArgs e)
        {
            Gao.Poker.Domain.Member member = dgMembers.SelectedItem as Gao.Poker.Domain.Member;
            if(member == null)
            {
                MessageBox.Show("请选择报名的会员","提示信息");
                return;
            }

            member = MemberService.Instance().GetMember(member.CardNo);

            CompetitionEntryService service = CompetitionEntryService.Instance();
            if (service.GetMember(member.CardNo, ViewModel.Competition.ID) != null)
            {
                MessageBox.Show("选择的会员已经报名","提示信息");
                return;
            }

            SeatingService ss = new SeatingService();
            var seats = ss.GetNullSeat(ViewModel.Competition.ID);
            if (seats.Count == 0 || ViewModel.Competition.MaxMember <= ViewModel.Members.Count)
            {
                MessageBox.Show("报名已满", "提示信息");
                return;
            }


            if (member.Points < ViewModel.Competition.SinglePoints)
            {
                MessageBox.Show("当前积分不足以购买", "提示信息");
                return;
            }

            CompetitionEntry ce = new CompetitionEntry();
            ce.MemberCardNo = member.CardNo;
            ce.CompetitionID = ViewModel.Competition.ID;
            ce.CreateDt = DateTime.Now;
            
            int indext = new Random().Next(0, seats.Count);
            Seating seat = seats[indext];
            seat.MemberCardNo = member.CardNo;
            ss.Save(seat);
          
            service.Save(ce);

            var competitionEntry = service.GetMember(member.CardNo, ViewModel.Competition.ID);

            BuyRecord record = new BuyRecord();
            record.BuyType = 0;
            competitionEntry.BuyNumber += 1;
            competitionEntry.BuyMoney += ViewModel.Competition.SinglePoints;

            ViewModel.Competition.BuyNumber += 1;
            ViewModel.Competition.TotalBet += ViewModel.Competition.Chips;
            ViewModel.Competition.TotalPoints += ViewModel.Competition.SinglePoints;
            member.Points -= ViewModel.Competition.SinglePoints;
            record.CardNo = member.CardNo;
            record.ConsumptionID = ViewModel.Competition.ID;
            record.Several = 1;
            record.CreateDt = DateTime.Now;
            //购买记录
            BuyRecordService.Instance().Save(record);
            //扣钱
            MemberService.Instance().Save(member);

            ViewModel.Competition = CompetitionService.Instance().Save(ViewModel.Competition);

            service.Save(competitionEntry);

            //CompetitionPrint print = new CompetitionPrint();
            PrintHelper.Instance().CompetitionPrint(member.Name, member.CardNo, ViewModel.Competition.Name, "报名", ViewModel.Competition.SinglePoints.ToString(), member.Points.ToString(), String.Format("{0}桌{1}座", seat.TableNum, seat.SeatingNum));

            //PrintDialog dialog = new PrintDialog();

            //if (dialog.ShowDialog() == true)
            //{
            //    dialog.PrintVisual(print, "Competition Print");
            //}

            ViewModel.OnPropertyChanged("Members");
            CallBack(true);
        }

        void Search_Click(object sender, RoutedEventArgs e)
        {
            MemberService service = new MemberService();
            var list = service.SearchMembers(txtName.Text, txtCardNo.Text);

            dgMembers.ItemsSource = list;
        }
    }
}
